package com.shine.framework.httpService
{
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.http.HTTPService;

	public class BasePostHttpService
	{
		private var xmlService:HTTPService;
		private var method:Function;
		private var failMethod:Function;
		public var cleanCache:String="off";
		public var debug:String="off";

		public function BasePostHttpService()
		{
		}

		public function getResultXml(xmlUrl:String, method:Function, postValue:Object=null, failMethod:Function=null):void
		{
			this.method=method;
			this.failMethod=failMethod;
			this.debug=debug;
			xmlService=new HTTPService();
			xmlService.method="post";
			xmlService.useProxy=false;
			xmlService.resultFormat=HTTPService.RESULT_FORMAT_XML;
			xmlService.addEventListener("result", httpResult);
			if (failMethod != null)
				xmlService.addEventListener(FaultEvent.FAULT, failResult);
			if (cleanCache == "on")
			{
				xmlUrl=xmlUrl + String.fromCharCode(0038) + "random=" + String(Math.random() * 10);
			}
			xmlService.url=xmlUrl;
			xmlService.send(postValue);
		}

		private function httpResult(event:ResultEvent):void
		{
			resultXml(XML(event.result));
		}

		private function failResult(event:FaultEvent):void
		{
			if (failMethod != null)
				failMethod.call(this);
		}

		protected function resultXml(value:XML):void
		{
			if (debug == "on")
			{
				Alert.show(value.toXMLString());
			}
			method.call(this, value);
		}

	}
}